const api = require('../../../api/mop_api');
import { toast, log, err } from '../../../utils/wxapi';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 20,
    list: [],
    user: {},
  },
  onShow() {
    this.points();
  },
  async points() {
    const { page, size } = this.data;
    try {
      const res = await api.moppoints({ page, size });
      if (res.respCode === 200) {
        const list = JSON.parse(res.source);
        log(list);
        const pageTotal = res.pageTotal;
        if (this.data.list.length === 0) {
          this.setData({ list, pageTotal });
        } else {
          let newlist = [...this.data.list, ...list];
          this.setData({ list: newlist, pageTotal });
        }
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { list, pageTotal, page } = this.data;
    if (list.length < pageTotal) {
      this.setData({
        page: page + 1,
      });
      this.points();
    } else {
      toast('已加载全部数据');
    }
  },
});
